//
//  BVTabBarViewController.m
//  AFNetworking
//
//  Created by 崔晋 on 2019/8/11.
//

#import "BVTabBarViewController.h"
#import "BVBaseViewController.h"
#import "BVBaseRouter.h"


@interface BVTabBarViewController ()<UITabBarDelegate, UITabBarControllerDelegate, UIImagePickerControllerDelegate>



@end

@implementation BVTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setValue:self.bvTabBar forKey:@"tabBar"];
    self.delegate = self;
    // Do any additional setup after loading the view.
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController {
    NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
    if (index == 2) {
        [BVBaseRouter presentViewControllerWithUrl:@"home/qrcode_actual" properties:@{@"delegate": self}];
        return NO;
    }
    self.bvTabBar.page = index;
    return YES;
}

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    NSURL *url = info[UIImagePickerControllerMediaURL];
    NSString *urlStr = [url.absoluteString substringFromIndex:7];
    UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        NSLog(@"保存视频失败%@", error.localizedDescription);
    }
    else {
        NSLog(@"保存视频成功");
    }
  
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    UINavigationController *navVc = self.childViewControllers[self.selectedIndex];
    if (![navVc isKindOfClass:[UINavigationController class]]) {
        return UIInterfaceOrientationMaskPortrait;
    }
    UIViewController *vc = navVc.visibleViewController;
    return vc.supportedInterfaceOrientations;
}

- (BOOL)shouldAutorotate {
    return YES;
}
    

- (BVTabBar *)bvTabBar {
    if (!_bvTabBar) {
        _bvTabBar = [[BVTabBar alloc] init];
        _bvTabBar.delegate = self;
    }
    return _bvTabBar;
}


@end
